package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


import cn.leancloud.LCException;
import cn.leancloud.LCObject;
import cn.leancloud.LCQuery;
import cn.leancloud.callback.FindCallback;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class RegistActivity extends AppCompatActivity {

    private EditText et_username;
    private EditText et_password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);


        //初始化控件
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);



        //返回
        findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //销毁当前页面
                finish();
            }
        });

        findViewById(R.id.register_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = et_username.getText().toString();
                String password = et_password.getText().toString();

                if(TextUtils.isEmpty(username) && TextUtils.isEmpty(password)){
                    Toast.makeText(RegistActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }else{

                    //查询是否有同名用户
                    LCQuery<LCObject> row = new LCQuery<>("UserTable");
                    row.whereEqualTo("username", username);
                    row.countInBackground().subscribe(new Observer<Integer>() {
                        public void onSubscribe(Disposable disposable) {}
                        public void onNext(Integer count) {
                            if(count==0){

                                // 构建对象
                                LCObject todo = new LCObject("UserTable");
                                // 为属性赋值
                                todo.put("username", username);
                                todo.put("password", password);
                                // 将对象保存到云端
                                todo.saveInBackground().subscribe(new Observer<LCObject>() {
                                    public void onSubscribe(Disposable disposable) {}
                                    public void onNext(LCObject todo) {
                                        // 成功保存之后，执行其他逻辑
                                        Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                        finish();
                                    }
                                    public void onError(Throwable throwable) {
                                        // 异常处理
                                    }
                                    public void onComplete() {}
                                });
                            }else if(count >= 1){
                                Toast.makeText(RegistActivity.this, "用户名已被占用", Toast.LENGTH_SHORT).show();
                            }

                        }
                        public void onError(Throwable throwable) {}
                        public void onComplete() {}
                    });

                }
            }
        });


        //点击注册
//        findViewById(R.id.register_btn).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                 String username = et_username.getText().toString();
//                 String password = et_password.getText().toString();
//
//                 if(TextUtils.isEmpty(username) && TextUtils.isEmpty(password)){
//                     Toast.makeText(RegistActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
//                 }else{
//
//                     int row = UserDbHelper.getInstance(RegistActivity.this).register(username, password);
//                     if(row>0){
//                         // 构建对象
//                         LCObject todo = new LCObject("Todo");
//                         // 为属性赋值
//                         todo.put("username", username);
//                         todo.put("password", password);
//                         // 将对象保存到云端
//                         todo.saveInBackground().subscribe(new Observer<LCObject>() {
//                             public void onSubscribe(Disposable disposable) {}
//                             public void onNext(LCObject todo) {
//                                 // 成功保存之后，执行其他逻辑
//                                 Toast.makeText(RegistActivity.this, "成功注册到云端", Toast.LENGTH_SHORT).show();
//                             }
//                             public void onError(Throwable throwable) {
//                                 // 异常处理
//                             }
//                             public void onComplete() {}
//                         });
//                         Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
//                         finish();
//                     }else if(row == -1){
//                         Toast.makeText(RegistActivity.this, "用户名已被占用", Toast.LENGTH_SHORT).show();
//                     }
//
//                 }
//            }
//        });
    }
}